import java.awt.GridLayout;
import java.awt.event.*;

import java.awt.*;
import javax.swing.*;



public class GUIDoctor extends JPanel {
	
	private JPanel panel;
	private JPanel leftside;
	private JPanel center;
	private JLabel welcome;
	private JScrollPane allPane;
	private MemberData array;
	private Doctor current;
	private FullListListener fll = new FullListListener();
	private DocListListener dll = new DocListListener();
        private JTabbedPane jPane;
	
	public GUIDoctor(Doctor d, MemberData a, JTabbedPane j)
	{
                jPane = j;
                array = a;
		current = d;
		panel = new JPanel();
		panel.setLayout(new BorderLayout());
		leftside = new JPanel();
		leftside.setLayout(new BoxLayout(leftside, BoxLayout.Y_AXIS));
                leftside.setBorder(BorderFactory.createEtchedBorder());
		leftside.add(new JLabel("All Patients"));
		center = new JPanel();
		center.setLayout(new BoxLayout(center, BoxLayout.Y_AXIS));
		center.add(new JLabel("Your Patients"));
                center.setBorder(BorderFactory.createEtchedBorder());
		
		
		
		
		allPane = new JScrollPane();
		
		welcome = new JLabel("Select Patients");
		
		for(int i = 0; i < array.getSize(); i++)
		{
			JButton temp = new JButton(array.getMemberAtIndex(i).toString());
			if(array.getMemberAtIndex(i) instanceof Doctor || array.getMemberAtIndex(i) instanceof Nurse)
			{
				temp.setEnabled(false);
			}
			temp.addActionListener(fll);
			leftside.add(temp);
		
		}
		
		for(int i = 0; i< current.getSize(); i++)
		{
                        int index = current.getPatientAt(i);
			JButton temp = new JButton(array.getMember(index).toString());
			temp.addActionListener(dll);
			center.add(temp);
			
			
		}
		panel.add(center, BorderLayout.EAST);
		panel.add(leftside, BorderLayout.WEST);
		panel.add(welcome, BorderLayout.NORTH);
		
		this.add(panel);	
	}
	
	public void updatefull()
	{
                leftside.removeAll();
                center.removeAll();
                leftside.add(new JLabel("All Patients"));
                center.add(new JLabel("Your Patients"));
		for(int i = 0; i < array.getSize(); i++)
		{
			JButton temp = new JButton(array.getMemberAtIndex(i).toString());
			if(array.getMemberAtIndex(i) instanceof Doctor || array.getMemberAtIndex(i) instanceof Nurse)
			{
				temp.setEnabled(false);
			}
			temp.addActionListener(fll);
			leftside.add(temp);
		}
		
		for(int i = 0; i< current.getSize(); i++)
		{
                        int index = current.getPatientAt(i);
			JButton temp = new JButton(array.getMember(index).toString());
			temp.addActionListener(dll);
			center.add(temp);
		}
                
		
	}
	
	
	//Listens to the Member buttons in the Full List of members
	private class FullListListener implements ActionListener
	{
		public void actionPerformed(ActionEvent event)
		{
                    //just checking for JButton because I have to cast to Button
			if(event.getSource() instanceof JButton)
			{
				//grabs string from JButton text
				String fullstring = ((JButton)event.getSource()).getText();
				//grabs username from string
				String[] un = fullstring.split(":");
				
				//I had to reset the text on the first button for it to work for some reaosn....
				((JButton)event.getSource()).setText("null");
				((JButton)event.getSource()).setText(fullstring);
				//gets user by username from list
				Member pat = array.getMember(un[0]);
				
				if(pat instanceof Patient)
				{
                                    
                                    if(!current.hasPatient(pat.getID()))
                                    
					//sets text on button
					current.addPatient((Patient)pat);
					updatefull();
				}
			}
		}
	}
	
	private class DocListListener implements ActionListener
	{
		public void actionPerformed(ActionEvent event)
		{
			if(event.getSource() instanceof JButton)
			{
				//grabs string from JButton text
				String fullstring = ((JButton)event.getSource()).getText();
				//grabs username from string
				String[] un = fullstring.split(":");
				
				//I had to reset the text on the first button for it to work for some reaosn....
				((JButton)event.getSource()).setText("null");
				((JButton)event.getSource()).setText(fullstring);
				//gets user by username from list
				Member pat = array.getMember(un[0]);
				
                                //clears previous patient tabs
                                while(jPane.getTabCount() > 3)
                                {
                                    jPane.remove(3);
                                }
                              
                                //create new "edit patient health info tab" and adds it to jPane
                                GUIDoctorPatient gdp = new GUIDoctorPatient((Patient)pat, jPane);
                                jPane.add(pat.getFirstName(), gdp);
				
			}	
				
		}
	}
	

}
